package com.xt.website.dao;

import com.xt.website.po.News;
import com.xt.website.po.NewsExt;
import org.apache.ibatis.annotations.*;

import java.util.List;

/*
 * @Author 爱尚实训赵国鑫
 * @ClassName  NewsDao
 * @Date 2019/8/18/13:38
 * @Description TODO:(一句话描述这个类)
 */
@Mapper
public interface NewsDao {

    /**
     *  根据一级类目查询全部新闻
     * @param cid
     */
    @Select("select tb_news.* from tb_news " +
            "inner join tb_categorysecond " +
            "on tb_news.csid=tb_categorysecond.id " +
            "inner join tb_category " +
            "on tb_category.id=tb_categorysecond.cid " +
            "where tb_category.id=#{cid} order by create_time desc")
    @Results(id = "newsMap",value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "title", property = "title"),
            @Result(column = "content", property = "content"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "csid", property = "csid"),
            @Result(column = "uid", property = "uid")
    })
    List<News> findAllNewsByCid(Integer cid);

    /**
     * 根据二级类目ID查询全部新闻
     * @param csid
     * @return
     */
    @Select("select tb_news.* from tb_news " +
            "inner join tb_categorysecond " +
            "on tb_news.csid=tb_categorysecond.id " +
            "where tb_news.csid=#{csid} order by create_time desc")
    @ResultMap("newsMap")
    List<News> findAllNewsByCsid(Integer csid);


    /**
     * 根据新闻id获取新闻详情
     * @param id   新闻id
     * @return
     */
    @Select("select id,title,content,create_time as createTime,csid,uid  from tb_news where id=#{id}")
    NewsExt findNewsByID(Integer id);

    /**
     * 根据新闻id获取下一条记录
     * @param id
     * @return
     */
    @Select("select id,title from tb_news where id = (select id from tb_news where id <#{id} order by create_time desc limit 1)")
    News finNextNewsByID(Integer id);

    @Select("select id,title from tb_news where id = (select id from tb_news where id <#{id} and csid=#{csid} order by create_time desc limit 1)")
    News finNextNewsByIDAndCsid(News news);


    /**
     * 根据新闻id获取上一条记录
     * @param id
     * @return
     */
    @Select("select id,title from tb_news where id = (select id from tb_news where id >#{id} order by create_time asc limit 1)")
    News finPrevNewsByID(Integer id);

    @Select("select id,title from tb_news where id = (select id from tb_news where id >#{id} and csid=#{csid} order by create_time asc limit 1)")
    News finPrevNewsByIDAndCsid(News news);


}
